<?php

namespace Home\Controller;

use Think\Controller;

class SqlController extends Controller
{
    public function _initialize()
    {
        if (empty($_SESSION['empid'])) {
            //$this->error('页面加载中。。。',U('Login/Login'),1);
            $this->redirect('Login/Login2');
        }
    }

    public function Sql()
    {
        if (IS_POST) {
            $sql = $_POST['sql'];
            $sql = str_replace("’", "'", $sql);
            $sql = str_replace("‘", "'", $sql);
            $sql = str_replace("\'", "'", $sql);
            if (empty($sql)) {
                $this->error('对不起字段不能为空');
            } elseif (!strstr(I('post.sql'), 'select')) {
                $this->error('对不起,sql语句错误');
            } else {
                $dao = M();
                $list = $dao->query("" . $sql . "");
                foreach ($list[0] as $k => $v) {
                    $s[] = $k;
                }
//var_dump($list);
//                print_r($s);exit;
                session('key', $s);
                session('sql', $sql);
                $this->assign('sql', $sql);
                $this->assign('list', $list);
                $this->display();
            }
        } else {
            $this->display();
        }
    }

    public function out()
    {
        $sql = session('sql');
        $key = session('key');
        foreach ($key as $s) {
            $sc[] = $s;
        }
        R('Excel/Out', array($sql, 0, $sc));
    }

    //模糊查询学员
    public function Studentlist(){
        $name = $_POST['student_name'];
        if(empty($name)){
            $this->error('查询条件不能为空');
        }
        $Student = M("CStudent"); // 实例化User对象
        $data['loginname'] = array('like', "%$name%");
        $list = $Student->where($data)->select();
        $sql = $Student->getLastSql();
        $sql = strtolower($sql);
        session('sql', $sql);
        header("Content-type:text/html;charset=utf-8");
//        dump($list);exit;
        $this->assign('sql', $sql);
        $this->assign('list', $list);
        $this->display('sql');
    }

    //模糊查询班次
    public function Schedulelist(){
        $name = $_POST['schedule_name'];
        if(empty($name)){
            $this->error('查询条件不能为空');
        }
        $Schedule = M("CSchedule"); // 实例化User对象
        $data['schename'] = array('like', "%$name%");
        $list = $Schedule->where($data)->select();
        $sql = $Schedule->getLastSql();
        $sql = strtolower($sql);
        session('sql', $sql);
        header("Content-type:text/html;charset=utf-8");
//        dump($list);exit;
        $this->assign('sql', $sql);
        $this->assign('list', $list);
        $this->display('sql');
    }

    //按班次编号查询学员
    public function Studentlist2(){
        $name = $_POST['class_num'];
        if(empty($name)){
            $this->error('查询条件不能为空');
        }
        $Student = M("CStudent"); // 实例化User对象
        $data['classid'] = array('like', "%$name%");
        $list = $Student->where($data)->select();
        $sql = $Student->getLastSql();
        $sql = strtolower($sql);
        session('sql', $sql);
        header("Content-type:text/html;charset=utf-8");
//        dump($list);exit;
        $this->assign('sql', $sql);
        $this->assign('list', $list);
        $this->display('sql');
    }
}

